<template>
  <el-dialog
    title="添加角色"
    width="600px"
    :visible.sync="dialogVisible"
    class="role-add-container"
  >
    <el-form
      ref="createForm"
      :model="formData"
      label-width="120px"
      :show-message="false"
    >
      <el-form-item
        label="名称"
        prop="name"
        required
      >
        <el-tooltip
          class="el-tooltip"
          content="用户角色的名称"
          effect="light"
        >
          <i class="el-icon-question"></i>
        </el-tooltip>
        <el-input
          v-model.trim="formData.name"
          auto-complete="off"
          placeholder="请输入角色名称"
          class="input"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="描述"
        prop="description"
        required
      >
        <el-tooltip
          class="el-tooltip"
          content="每个角色的信息描述"
          effect="light"
        >
          <i class="el-icon-question"></i>
        </el-tooltip>
        <el-input
          v-model.trim="formData.description"
          auto-complete="off"
          placeholder="请输入角色描述"
          class="input"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="角色权限列表"
        prop="guardList"
        required
      >
        <el-tooltip
          class="el-tooltip"
          content="每个角色的权限列表"
          effect="light"
        >
          <i class="el-icon-question"></i>
        </el-tooltip>
        <el-select
          multiple
          v-model="formData.guardList"
          placeholder="请选择权限"
          class="select"
        >
          <el-option
            v-for="item in guardList"
            :key="item.uuid"
            :label="item.name"
            :value="item.uuid"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button
        type="primary"
        @click="addRole"
        :loading="btnLoading"
      >确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getGuardListApi } from '../../../api/guard';
import { createRoleApi } from '../../../api/role';
import { parseError } from '../../../utils';
import { Message } from 'element-ui';
import { validateForm } from './service';

export default {
  data() {
    return {
      guardList: [],
      dialogVisible: false,
      formData: {
        name: '',
        description: '',
        guardList: []
      },
      btnLoading: false
    };
  },
  watch: {
    dialogVisible: {
      handler(value) {
        if (!value) this.$refs.createForm.resetFields();
      }
    }
  },
  methods: {
    showDialog() {
      getGuardListApi()
        .then(res => (this.guardList = res))
        .catch(error => Message.error(parseError(error)));
      this.dialogVisible = true;
    },
    addRole() {
      const validError = validateForm(this.formData);
      if (validError) return Message.warning(validError);
      this.btnLoading = true;
      createRoleApi(this.formData.name, this.formData.description, this.formData.guardList)
        .then(res => {
          this.dialogVisible = false;
          this.$emit('refresh');
          Message.success('创建角色成功');
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.btnLoading = false));
    }
  }
};
</script>

<style lang="scss" scoped>
.el-tooltip {
  margin-right: 10px;
}
.role-add-container {
  .input {
    width: 300px;
  }
  .select {
    width: 300px;
  }
}
</style>
